package tv.youxing.android.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import java.io.File;

import tv.youxing.android.R;
import tv.youxing.android.application.BaseApplication;
import tv.youxing.android.utils.L;

/**
 * Created by Administrator on 2017/7/3.
 */

public class LocalVideoPlayActivity extends AppCompatActivity {


    VideoView mVideoView;
    MediaController mediaco;

    boolean fullscreen = false;
    String videoFileName;//文件名称

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_play);
        mVideoView=(VideoView)findViewById(R.id.video_view);
        mediaco=new MediaController(this);

        videoFileName = getIntent().getStringExtra("filename");

//隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        if(!fullscreen){//设置RelativeLayout的全屏模式
            RelativeLayout.LayoutParams layoutParams=
                    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            mVideoView.setLayoutParams(layoutParams);

            fullscreen = true;//改变全屏/窗口的标记
        }else{//设置RelativeLayout的窗口模式
            RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(300,400);
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            mVideoView.setLayoutParams(lp);
            fullscreen = false;//改变全屏/窗口的标记
        }



        File file=new File(videoFileName);
        if(file.exists()){
            //VideoView与MediaController进行关联  
            mVideoView.setVideoPath(file.getAbsolutePath());
            mVideoView.setMediaController(mediaco);
            mediaco.setMediaPlayer(mVideoView);
            //让VideiView获取焦点  
            mVideoView.requestFocus();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (mVideoView != null) {
            mVideoView.pause();
            BaseApplication.mLocalVideoPos.put(1,mVideoView.getCurrentPosition());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (mVideoView != null) {

            if (BaseApplication.mLocalVideoPos.containsKey(1)) {
                mVideoView.seekTo(BaseApplication.mLocalVideoPos.get(1));
                mVideoView.start();
            }
        }

    }
}
